﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;



namespace Battle
{
    public partial class MainForm : Form
    {
        private readonly Fighter _humanPlayer;
        private readonly Fighter _computerPlayer;

        private readonly Tablee _humanBoard;
        private readonly Tablee _computerBoard;

        private readonly FightController _controller;

        private readonly ScoredTable _scoreboard;

        private readonly Button _shuffleButton;
        private readonly Button _startFightButton;
        private readonly Button _newFightButton;

        private static readonly Color ColorForButton = Color.FromArgb(45, 177, 252);

        private SoundPlayer soundPlayer; 
        private WinerForm winerForm;
        private bool startButtonClicked = false;


        public MainForm(SettingsForm settings)
        {
            SuspendLayout();
            InitializeComponent();

            soundPlayer = new SoundPlayer(Resource1.Ocean);
            soundPlayer.PlayLooping();

            _humanBoard = new Tablee(settings, true);
            _computerBoard = new Tablee(settings,false);

            _humanPlayer = new FighterHuman(settings.textBox1.Text, _computerBoard);
            _computerPlayer = new FighterR2D2("R2D2");

            _scoreboard = new ScoredTable(_humanPlayer, _computerPlayer, 10);
            _controller = new FightController(_humanPlayer, _computerPlayer, _humanBoard, _computerBoard, _scoreboard);

            _shuffleButton = CreateButtonText("Set", ColorForButton);
            _newFightButton = CreateButtonImage(3);
            _startFightButton = CreateButtonImage(1);

            SetupWindow();
            LayoutControls();

            _scoreboard.FightEnded += OnFightEnded;

            _shuffleButton.Click += OnShuffleButtonClick;
            _startFightButton.Click += OnStartFightButtonClick;
            _newFightButton.Click += OnNewFightButtonClick;

            ResumeLayout();

            StartNewFight();           
        }

        public MainForm(WinerForm winerForm)
        {
            // TODO: Complete member initialization
            this.winerForm = winerForm;
        }

        private void OnNewFightButtonClick(object sender, System.EventArgs e)
        {
            StartNewFight();
        }

        private void StartNewFight()
        {
            _shuffleButton.Visible = true;
            _startFightButton.Visible = true;
            _newFightButton.Visible = false;
            _controller.NewFightOnController();
            soundPlayer.PlayLooping();
        }

        private void OnStartFightButtonClick(object sender, System.EventArgs e)
        {
            _shuffleButton.Visible = false;
            _newFightButton.Visible = false;
            _startFightButton.Visible = false;
            _controller.StartFight();
            soundPlayer.PlayLooping();
            startButtonClicked = true;
        }

        private void OnShuffleButtonClick(object sender, System.EventArgs e)
        {
            _humanBoard.AddRndShips();
        }

        private void OnFightEnded(object sender, System.EventArgs e)
        {
            _shuffleButton.Visible = false;
            _startFightButton.Visible = false;
            _newFightButton.Visible = true;
            _computerBoard.ShowShips();
            startButtonClicked = false;
        }

        private void SetupWindow()
        {
            AutoScaleDimensions = new SizeF(8, 19);
            AutoScaleMode = AutoScaleMode.Font;
            Font = new Font("Calibri", 10, FontStyle.Regular, GraphicsUnit.Point, 186);
            Margin = Padding.Empty;
            Text = "BattleShip";
            BackgroundImage = Resource1.images;
            FormBorderStyle = FormBorderStyle.FixedSingle;
            StartPosition = FormStartPosition.CenterScreen;
            MaximizeBox = false;
        }

        private static Button CreateButtonText(string text, Color backColor)
        {
            var button = new Button
            {
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.White,
                BackColor = backColor,
                UseVisualStyleBackColor = false,
                Size = new Size(60, 40),
                Text = text,
                Font = new System.Drawing.Font("Arial Unicode MS", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))),
                TextAlign = ContentAlignment.TopCenter,
            };
            button.FlatAppearance.BorderSize = 0;

            return button;
        }

        private static Button CreateButtonImage(int i)
        {
            if (i == 3)
            {
                var button = new Button
                {
                    FlatStyle = FlatStyle.Flat,
                    Size = new Size(100, 50),   
                    BackgroundImage = Resource1._3,
                    UseVisualStyleBackColor = true,
                };
                button.FlatAppearance.BorderSize = 0;

                return button;
            }
            var button1 = new Button
              {
                  FlatStyle = FlatStyle.Flat,
                  Size = new Size(100, 50),   
                  BackgroundImage = Resource1._1,
                  UseVisualStyleBackColor = true,
              };
                button1.FlatAppearance.BorderSize = 0;
                return button1;
        }


        private void LayoutControls()
        {
            _humanBoard.Location = new Point(10, 50);
            _computerBoard.Location = new Point(_humanBoard.Right+50, 50);
            _scoreboard.Location = new Point(50, _humanBoard.Bottom);
            _scoreboard.Width = _computerBoard.Right - 25;
            _newFightButton.Location = new Point(_computerBoard.Right - _newFightButton.Width, _scoreboard.Bottom);
            _startFightButton.Location = _newFightButton.Location;
            _shuffleButton.Location = new Point(_newFightButton.Location.X - _shuffleButton.Width - 25, _newFightButton.Location.Y);

            Controls.AddRange(new Control[]
                                  {
                                      _humanBoard,
                                      _computerBoard,
                                      _scoreboard,
                                      _newFightButton,
                                      _startFightButton,
                                      _shuffleButton
                                  });

            ClientSize = new Size(_computerBoard.Right + 25, _startFightButton.Bottom + 25);
        }

        private void MainForm_Load(object sender, System.EventArgs e)
        {

        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.cheatermodeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.helpToolStripMenuItem,
            this.aboutToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(462, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newToolStripMenuItem,
            this.cheatermodeToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "File";
            // 
            // newToolStripMenuItem
            // 
            this.newToolStripMenuItem.Name = "newToolStripMenuItem";
            this.newToolStripMenuItem.Size = new System.Drawing.Size(146, 22);
            this.newToolStripMenuItem.Text = "New";
            this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
            // 
            // cheatermodeToolStripMenuItem
            // 
            this.cheatermodeToolStripMenuItem.Name = "cheatermodeToolStripMenuItem";
            this.cheatermodeToolStripMenuItem.Size = new System.Drawing.Size(146, 22);
            this.cheatermodeToolStripMenuItem.Text = "CheaterMode";
            this.cheatermodeToolStripMenuItem.Click += new System.EventHandler(this.cheatermodeToolStripMenuItem_Click);
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(146, 22);
            this.exitToolStripMenuItem.Text = "Exit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // helpToolStripMenuItem
            // 
            this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
            this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
            this.helpToolStripMenuItem.Text = "Help";
            this.helpToolStripMenuItem.Click += new System.EventHandler(this.helpToolStripMenuItem_Click);
            // 
            // aboutToolStripMenuItem
            // 
            this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
            this.aboutToolStripMenuItem.Size = new System.Drawing.Size(52, 20);
            this.aboutToolStripMenuItem.Text = "About";
            this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
            // 
            // MainForm
            // 
            this.ClientSize = new System.Drawing.Size(462, 444);
            this.Controls.Add(this.menuStrip1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "MainForm";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingsForm a = new SettingsForm();
            a.Show();
            this.Visible = false;
            a.soundPlayerSettings.Play();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 a = new AboutBox1();
            a.Show();
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HelpForm q = new HelpForm();
            q.Show();
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;            
        }

        private void cheatermodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (startButtonClicked)
            {
                _computerBoard.ShowShips();
            }
            else
            {
                MessageBox.Show("Press start");
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.ExitThread();
        }
    }
}
